package com.doit.day07

import org.apache.spark.rdd.RDD
import org.apache.spark.sql.{DataFrame, Row, SparkSession}
import org.apache.spark.sql.expressions.Window
import org.apache.spark.sql.types.{DataTypes, StructType}

/**
 * @Author:
 * @WX: 17710299606
 * @Tips: 学大数据 ,到多易教育
 * @DOC: https://blog.csdn.net/qq_37933018?spm=1000.2115.3001.5343
 * @Description:
 */
object Demo04DF2RDD {
  def main(args: Array[String]): Unit = {

    val session = SparkSession.builder()
      .appName("test")
      .master("local[*]")
      .getOrCreate()

    import org.apache.spark.sql.functions._
    import session.implicits._

    val structType = new StructType()
      .add("oid", DataTypes.StringType)
      .add("price", DataTypes.DoubleType)
      .add("city", DataTypes.StringType)
      .add("category", DataTypes.StringType)
      .add("id", DataTypes.StringType)
    // 加载订单
    val orderDF: DataFrame = session.read.schema(structType).csv("data/orders/order.csv")

    /**
     *
     *   DataFrame = Dataset[Row]
     * root
     *  |-- oid: string (nullable = true)
     *  |-- price: doublllable = true)
     *  |-- city: string (e (nunullable = true)
     *  |-- category: string (nullable = true)
     *  |-- id: string (nullable = true)
     *
     */
    orderDF.printSchema()
    val rdd: RDD[Row] = orderDF.rdd
    // 解析出Row的结构  获取对应属性数据 才能处理  toDF


    rdd.map(row=>{
    /*  row.getString(1)
      row.getDouble(2)
      row.getString(3)*/
      // def  getAs[T]() :T={}
      val oid  = row.getAs[String]("oid")
      val price  = row.getAs[Double]("price")
      val city = row.getAs[String]("city")
      val category = row.getAs[String]("category")
      (oid , price , category ,city )
      }).foreach(println)

    /**
     * 模式匹配  匹配完整的Row结构
     */
    rdd.map(row=>{
      row match {
        case Row(oid:String , price:Double , city:String , category:String,id:String)=>(oid, price)
      }
    }).foreach(println)


    rdd.map({
        case Row(oid: String, price: Double, city: String, category: String, id: String) => (oid, category)
      }).foreach(println)


  }

}
